package com.northwest_man.react_native;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.IOFileFilter;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SdkVersion {
    private static void handleMatcher(final HashMap<String, Integer> sdkMap, final Matcher implementationMatcher1) {
        while (implementationMatcher1.find()) {
            final String sdk = implementationMatcher1.group(1);
            final Pattern sdkPattern = Pattern.compile("([a-z.:]+):([0-9.]+)");
            final Matcher sdkMatcher = sdkPattern.matcher(sdk);
            if (sdkMatcher.find()) {
                final String name = sdkMatcher.group(1);
                final String version = sdkMatcher.group(2);
                final String key = String.format("%s:%s", name, version);
                if (sdkMap.containsKey(key)) {
                    sdkMap.put(key, sdkMap.get(key) + 1);
                } else {
                    sdkMap.put(key, 1);
                }
            }
        }
    }

    public static void main(String[] args) {
        final IOFileFilter dirFilter = new IOFileFilter() {
            @Override
            public boolean accept(File file) {
                System.out.println(String.format("dirFilter, accept---->file: [%s]", file.getAbsolutePath()));
                return false;
            }

            @Override
            public boolean accept(File file, String s) {
                System.out.println(String.format("dirFilter, accept---->file: [%s]", file.getAbsolutePath()));
                return false;
            }
        };
        final IOFileFilter fileFilter = new IOFileFilter() {
            @Override
            public boolean accept(File file) {
                System.out.println(String.format("fileFilter, accept---->file: [%s]", file.getAbsolutePath()));
                return false;
            }

            @Override
            public boolean accept(File file, String s) {
                System.out.println(String.format("fileFilter, accept---->file: [%s], s: [%s]", file.getAbsolutePath(), s));
                return false;
            }
        };
//        Collection<File> fileList = FileUtils.listFilesAndDirs(new File("/Users/h/Projects/app-rn/node_modules") , fileFilter, dirFilter);
        Collection<File> fileList = FileUtils.listFiles(new File("/Users/h/Projects/app-rn/node_modules"), new String[]{"gradle"}, true);
//        FileUtils.listFilesAndDirs()
        final HashMap<String, Integer> sdkMap = new HashMap<>();
        for (final File file : fileList) {
            System.out.println(file.getAbsolutePath());

            try {
                final String fileContent = FileUtils.readFileToString(file, Charset.defaultCharset());
                final Pattern pattern = Pattern.compile("dependencies\\s*(\\{[^}]+})");
                final Matcher matcher = pattern.matcher(fileContent);
                while (matcher.find()) {
                    final String dependencies = matcher.group(1);
                    System.out.println(dependencies);
                    // testImplementation 'org.robolectric:robolectric:4.3.1'
                    final Pattern implementationPattern1 = Pattern.compile("[a-z]+\\s+'([:a-z-.0-9]+?)'");
                    // testImplementation "org.robolectric:robolectric:4.3.1"
                    final Pattern implementationPattern2 = Pattern.compile("[a-z]+\\s+\"([:a-z-.0-9]+?)\"");
                    // testImplementation("org.robolectric:robolectric:4.3.1")
                    final Pattern implementationPattern3 = Pattern.compile("[a-z]+\\s*\\(\"([:a-z-.0-9]+?)\"\\)");
                    // testImplementation('org.robolectric:robolectric:4.3.1')
                    final Pattern implementationPattern4 = Pattern.compile("[a-z]+\\s*\\('([:a-z-.0-9]+?)'\\)");
                    final Matcher implementationMatcher1 = implementationPattern1.matcher(dependencies);
                    final Matcher implementationMatcher2 = implementationPattern2.matcher(dependencies);
                    final Matcher implementationMatcher3 = implementationPattern3.matcher(dependencies);
                    final Matcher implementationMatcher4 = implementationPattern4.matcher(dependencies);

                    handleMatcher(sdkMap, implementationMatcher1);
                    handleMatcher(sdkMap, implementationMatcher2);
                    handleMatcher(sdkMap, implementationMatcher3);
                    handleMatcher(sdkMap, implementationMatcher4);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        for (final Map.Entry<String, Integer> entry : sdkMap.entrySet()) {
            System.out.println(String.format("%60s%20d", entry.getKey(), entry.getValue()));
        }
    }
}
